<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiRewardVideoAd extends ApiCommon
{
	//获取广告位ID
	public function getunitid(){
		$hid = input('post.hid/d');
		if(!$hid) return $this->json(['status'=>0,'msg'=>'未找到该广告记录']);
		$info = Db::name('designerpage_rwvideoad')->where('aid',aid)->where('id',$hid)->find();
		if(!$info) return $this->json(['status'=>0,'msg'=>'未找到该广告记录']);
		return $this->json(['status'=>1,'unitid'=>$info['unitid']]);
	}
	//给奖励
	public function givereward(){
		$this->checklogin();
		$hid = input('post.hid/d');
		if(!$hid) return $this->json(['status'=>0,'msg'=>'未找到该广告记录']);
		$info = Db::name('designerpage_rwvideoad')->where('aid',aid)->where('id',$hid)->find();
		if(!$info) return $this->json(['status'=>0,'msg'=>'未找到该广告记录']);

		$givescore = $info['givescore'];

		$record = Db::name('designerpage_rwvideoad_record')->where('aid',aid)->where('mid',mid)->where('hid',$hid)->where('createdate',date('Y-m-d'))->find();
		if($record){
			if($record['givetimes'] >= $info['givetimes']) return $this->json(['status'=>0,'msg'=>'您今日已获得过奖励了']);
			Db::name('designerpage_rwvideoad_record')->where('id',$record['id'])->inc('givescore',$givescore)->inc('givetimes',1)->update();
		}else{
			$data = [];
			$data['aid'] = aid;
			$data['mid'] = mid;
			$data['hid'] = $hid;
			$data['givescore'] = $givescore;
			$data['givetimes'] = 1;
			$data['createdate'] = date('Y-m-d');
			$data['createtime'] = time();
			Db::name('designerpage_rwvideoad_record')->insert($data);
		}
		\app\common\Member::addscore(aid,mid,$givescore,'广告奖励');
		return $this->json(['status'=>1,'msg'=>'恭喜您获得'.$givescore.t('积分').'奖励']);
	}

}